/*
 * @Author: your name
 * @Date: 2021-03-15 15:50:34
 * @LastEditTime: 2021-12-16 11:50:10
 * @LastEditors: LVGRAPE
 * @Description: In User Settings Edit
 * @FilePath: \Mixly_FOR__ZINO\arduino\hardware\UAVI_TECH\ZINO\cores\ZINO_IAP\USART_Configuration.h
 */
#ifndef USART_CONFIGURATION_H
#define USART_CONFIGURATION_H
#ifdef __cplusplus
extern "C" {
#endif
#include "at32f4xx.h"

#include <stdbool.h>
#define HEX ((uint8_t)16)
#define BIN ((uint8_t)2)
#define DEC ((uint8_t)10)
#define OCT ((uint8_t)8)

#define serial serial1
#define Bluetooth serial2
#define IrDA serial3


#define USART_MAX_BUFFER_SIZE 512
// typedef enum {
// 	serial=0,
// 	Bluetooth,
// 	IrDA,
// 	serialCount,
// }LITWOOD_SerialNum_e;
typedef enum {
	serial1=0,
	serial2,
	serial3,
	serial4,
	serial5,
	serialCount,
}ZionSerialNum_e;
typedef enum {
	rxDone=1,
	txDone=1<<1,
	rxBusy=1<<2,
	txBusy=1<<3,
}SerialStatus_e;
typedef struct usartRT {
	uint8_t status;
	uint16_t rxBufferSize;
	uint16_t txBufferSize;
	uint8_t txBuffer[USART_MAX_BUFFER_SIZE];
	uint8_t rxBuffer[USART_MAX_BUFFER_SIZE];
}usartPacket_s;
typedef struct serialHandle {
	USART_Type* USARTx;
	DMA_Channel_Type* rxChannel;
	DMA_Channel_Type* txChannel;
	usartPacket_s packet;
	usartPacket_s DMApacket;
}SerialHandle_s;
#define UART_TX DMA_DIR_PERIPHERALDST
/*periph to memory*/
#define UART_RX DMA_DIR_PERIPHERALSRC

void USART_DMA_INIT(DMA_Channel_Type* DMAchannelx, uint32_t periphAddr, uint32_t bufferAddr, uint16_t bufferSize, uint32_t Direction);
void Serial_Setup(SerialHandle_s* serialx, uint32_t baudrate, uint16_t Wordlength, uint16_t Stopbits, uint16_t Parity, uint16_t Mode, uint16_t HardwareFlowControl);
void Serial_Begin(SerialHandle_s* serialx, uint32_t baudrate);
void Serial_putc(SerialHandle_s* serialx, const char b);
void Serial_puts(SerialHandle_s* serialx, const char* b);
void Serial_put(SerialHandle_s* serialx, const char* buffer, uint16_t lenght);
char Serial_getc(SerialHandle_s* serialx);
char Serial_get(SerialHandle_s* serialx, uint16_t byteIndex);
char* Serial_gets(SerialHandle_s* serialx);
uint16_t IsSerialRxHasData(SerialHandle_s* serialx);
void Serial_Purge(SerialHandle_s* serialx);
void Serial_flush(SerialHandle_s* serialx);
char* myitoa(int num, char* str, int radix);
void Serial_printf(SerialHandle_s* USARTx, const char* format, ...);
void Serial_printNum(SerialHandle_s* USARTx, uint8_t printType, double num, uint8_t NotInline);
extern SerialHandle_s serialHandle[serialCount];
void rt_kprintf(const char* format, ...);
void usartPrintNum(double num);
void uartPut(uint8_t *c, uint8_t len);
void usartPrintVariable(char *name, double value, bool _NotInline);
void BLE_PrintVariable(char *name, double value, bool _NotInline, bool printNow);
#ifdef __cplusplus
}
#endif
#endif // !USART_CONFIGURATION_H
